package com.qkst.website.web.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.CatgroupBean;
import com.qkst.website.backstage.bean.ProductBean;
import com.qkst.website.common.core.BaseController;
import com.qkst.website.common.core.Pager;
import com.qkst.website.web.bean.BargainLogBean;
import com.qkst.website.web.bean.CollectionBean;
import com.qkst.website.web.bean.CommentBean;
import com.qkst.website.web.bean.UserBean;
import com.qkst.website.web.service.BargainService;
import com.qkst.website.web.service.CollectionService;
import com.qkst.website.web.service.CommentService;
import com.qkst.website.web.service.ProductService;

/**
 * @author lihao
 * @description 产品控制器
 * @date 2017-05-11
 * @emial ahlxlihao@163.com
 *
 */
@Controller
@RequestMapping("/web/product")
public class WebProductController extends BaseController{


	@Autowired
	private ProductService productService;//产品 service
	
	@Autowired
	private CollectionService collectionService;//收藏 service
	
	@Autowired
	private CommentService commentService;//评论 service
	
	@Autowired
	private BargainService bargainService;//砍价 service
	
	/**
	 * 产品列表页面数据查找
	 * @param request
	 * @param response
	 * @param product
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/{partnum}/product.html", method = { RequestMethod.GET })
	public ModelAndView product(HttpServletRequest request,HttpServletResponse response,@PathVariable("partnum") String partnum) throws IOException{
		ModelAndView mv= this.webModelAndView("product/product");
		//查询产品根据编号
		ProductBean product=productService.queryProductByPartnum(partnum);
		UserBean loginUser=this.getLoginUser(request, response);
		//查询砍价金额
		BargainLogBean bargain = new BargainLogBean();
		bargain.setBuyer(loginUser);
		bargain.setProduct(product);
		bargain = bargainService.queryBargainLogByConditions(bargain);
		mv.addObject("product", product);//返回产品列
		mv.addObject("bargain", bargain);//返回砍价记录
		return mv;
	}
	
	/**
	 * 产品列表页面数据查找
	 * @param request
	 * @param response
	 * @param product
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.html", method = { RequestMethod.POST })
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,@RequestBody ProductBean product) throws IOException{
		ModelAndView mv= this.webModelAndView("product-list");
		Pager pager = new Pager();
		//分页查询产品列表
		List<ProductBean> productList=productService.queryProductList(product);
		//产品总记录数
		int records=productService.queryCount(product);
		pager.setRecords(records);
		mv.addObject("productList", productList);//返回产品列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	
	/**添加收藏
	 * @param request
	 * @param response
	 * @param collection
	 * @throws IOException
	 */
	@RequestMapping(value = "/collection/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> collection_add(HttpServletRequest request,HttpServletResponse response,CollectionBean collection) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		UserBean user=this.getLoginUser(request, response);
		collection.setUser(user);
		collection.setUserId(user.getUserId());
		//添加收藏
		collectionService.save(collection,result);
		return result;
	}
	
	/**取消收藏
	 * @param request
	 * @param response
	 * @param collection
	 * @throws IOException
	 */
	@RequestMapping(value = "/collection/cancel.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> collection_cancel(HttpServletRequest request,HttpServletResponse response,CollectionBean collection) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//取消收藏
		collectionService.cancel(collection,result);
		return result;
	}
	
	/**
	 * 收藏列表页面数据查找
	 * @param request
	 * @param response
	 * @param collection
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/collection/list.html", method = { RequestMethod.GET })
	public ModelAndView collection_list(HttpServletRequest request,HttpServletResponse response,CollectionBean collection) throws IOException{
		ModelAndView mv= this.webModelAndView("product/collection/list");
		collection.setPage(1);
		collection.setRows(10);
		Pager pager = new Pager();
		//分页查询收藏列表
		List<CollectionBean> collectionList=collectionService.queryCollectionList(collection);
		//收藏总记录数
		int records=collectionService.queryCount(collection);
		pager.setRecords(records);
		mv.addObject("collectionList", collectionList);//返回收藏列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	
	/**添加评论
	 * @param request
	 * @param response
	 * @param comment
	 * @throws IOException
	 */
	@RequestMapping(value = "/comment/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> comment_add(HttpServletRequest request,HttpServletResponse response,@RequestBody CommentBean comment) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		UserBean user=this.getLoginUser(request, response);
		comment.setUser(user);
		comment.setUserId(user.getUserId());
		//添加评论
		commentService.save(comment,result);
		return result;
	}
	
	/**删除评论
	 * @param request
	 * @param response
	 * @param comment
	 * @throws IOException
	 */
	@RequestMapping(value = "/comment/delete.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> comment_delete(HttpServletRequest request,HttpServletResponse response,CommentBean comment) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//删除评论
		commentService.delete(comment,result);
		return result;
	}
	
	
	/**
	 * 素材列表页面数据查找
	 * @param request
	 * @param response
	 * @param category
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/drew/{catgroupId}/list.html", method = { RequestMethod.GET })
	public ModelAndView drew_list(HttpServletRequest request,HttpServletResponse response,@PathVariable("catgroupId") Long catgroupId) throws IOException{
		ModelAndView mv= this.webModelAndView("drew/list");
		Pager pager = new Pager();
		ProductBean product = new ProductBean();
		product.setPage(1);
		product.setRows(10);
		CatgroupBean catgroup = new  CatgroupBean();
		catgroup.setCatgroupId(catgroupId);
		product.setCatgroup(catgroup);//设置目录
		//分页查询产品列表
		List<ProductBean> productList=productService.queryProductList(product);
		//产品总记录数
		int records=productService.queryCount(product);
		pager.setRecords(records);
		mv.addObject("productList", productList);//返回产品列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	

}
